package com.it.blog.service.impl;

import com.it.blog.dao.ArticleDao;
import com.it.blog.entity.Article;
import com.it.blog.entity.ArticleTag;
import com.it.blog.service.ArticleService;
import com.it.blog.tools.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 羡羡
 * @Date: 2022/02/01/16:01
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleDao articdao;

    @Override
    public int arcount() {
        //总共的位文章数
        int allnumber=articdao.arcount();
        return allnumber;
    }

    @Override
    public List artall() {
        List lis=articdao.artall();
        return lis;
    }

    @Override
    public List hotrec() {
        List lis=articdao.hotrec();
        return lis;
    }

    @Override
    public List indtype() {
        List indty=articdao.indtype();
        return indty;
    }

    @Override
    public List indexlable() {
        List indexlablelis=articdao.indexlable();
        return indexlablelis;
    }

    @Override
    public List typeall() {
        List lis= articdao.typeall();
        return lis;
    }

    @Override
    public List typeblog(int arid) {
        List lis=articdao.typeblog(arid);
        return lis;
    }

    @Override
    public List lableall() {
        List lis=articdao.lableall();
        return lis;
    }

    @Override
    public List Tagblog(int tagid) {
        List lis=articdao.Tagblog(tagid);
        return lis;
    }

    @Override
    public List<String> findyear() {
        List lis=articdao.findyear();
        return lis;
    }

    /**
     * 通过调用dao层方法拿到年份的集合，在遍历年份，查对应的博客列表并存放到map集合中。
     * 这里的map要使用LinkedHashMap<>()，这种是按存入的顺序取出，
     * 而HashMap<>()数据存入之后顺序会被打乱，这样在前端渲染的时候就会出问题。
     * @return
     */
    @Override
    public Map<String, List<Article>> findyearblog() {
        List<String> years = articdao.findyear();
        Map<String, List<Article>> map = new LinkedHashMap<>();
        for (String year : years){
            map.put(year, articdao.findyearblog(year));
        }
        return map;
    }

    @Override
    public int blogwacount() {
       int wacount= articdao.blogwacount();
        return wacount;
    }

    @Override
    public List searchlis(String queryvalue) {
        List lis= articdao.searchlis("%"+queryvalue+"%");
        return lis;
    }

    @Override
    public ArticleTag findblog(int blogid) {
        ArticleTag artag=articdao.findblog(blogid);
        String context=artag.getArticlecontext();
        System.out.println("ArticleServiceImpl 118:"+context);
        artag.setArticlecontext(MarkdownUtils.markdownToHtmlExtensions(context));
        return artag;
    }

    @Override
    public int watchnm(int blogid) {
        return articdao.watchnm(blogid);
    }

}
